<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
       <crudOperation :permission="permission" > 
        <el-tooltip eccect="light" placement="top" slot="right">
          <div slot="content">轮播图图片尺寸：1920*815像素</div>
          <el-link icon="el-icon-info" type="primary" :underline="true">说明</el-link>
        </el-tooltip>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="外链" prop="hrefLink">
            <el-input v-model="form.hrefLink" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="图片路径">
            <el-upload ref="upload" :limit="1" :before-upload="beforeUpload" :auto-upload="true" :headers="headers" :on-success="handleSuccess" :on-error="handleError" :action="fileUploadApi + '?name=' + imageName">
              <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <!-- <el-form-item label="图片路径" prop="imageUrl">
              <el-input v-model="form.imageUrl" style="width: 370px;" />
            </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" />
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="hrefLink" label="外链" />
        <el-table-column prop="imageUrl" label="图片路径">
          <template slot-scope="{row}">
            <el-image :src=" row.imageUrl" :preview-src-list="[row.imageUrl]" fit="contain" lazy class="el-avatar">
              <div slot="error">
                <i class="el-icon-document" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','swiperSetting:edit','swiperSetting:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSwiperSetting from '@/api/swiperSetting'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

const defaultForm = { id: null, title: null, hrefLink: '#', imageUrl: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'SwiperSetting',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '轮播图配置', url: 'api/swiperSetting', idField: 'id', sort: 'id,desc', crudMethod: { ...crudSwiperSetting }})
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'fileUploadApi'
    ])
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      loading: false,
      imageName: '',
      permission: {
        add: ['admin', 'swiperSetting:add'],
        edit: ['admin', 'swiperSetting:edit'],
        del: ['admin', 'swiperSetting:del']
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        hrefLink: [
          { required: true, message: '外链不能为空', trigger: 'blur' }
        ],
        imageUrl: [
          { required: true, message: '图片路径不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 上传文件
    upload() {
      this.$refs.upload.submit()
    },
    beforeUpload(file) {
      let isLt2M = true
      isLt2M = file.size / 1024 / 1024 < 100
      if (!isLt2M) {
        this.loading = false
        this.$message.error('上传文件大小不能超过 100MB!')
      }
      this.imageName = file.name
      return isLt2M
    },
    handleSuccess(response, file, fileList) {
      console.log(response)
      this.crud.notify('上传成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      this.$refs.upload.clearFiles()
      // this.crud.status.add = CRUD.STATUS.NORMAL
      // this.crud.resetForm()
      // this.crud.toQuery()
      this.form.imageUrl = this.baseApi + '/file/' + response.type + '/' + response.realName
    },
    // 监听上传失败
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.$notify({
        title: msg.message,
        type: 'error',
        duration: 2500
      })
      this.loading = false
    }
  }
}
</script>

<style scoped>
 ::v-deep .el-image__error,
.el-image__placeholder {
  background: none;
}

 ::v-deep .el-image-viewer__wrapper {
  top: 55px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 370px;
  height: 190px;
  display: block;
}
</style>
